package com.his.modules.sysjob.job;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.core.utils.HttpUtils;
import com.his.core.utils.StringUtils;
import com.his.modules.business.entity.CaSynSignData;
import com.his.modules.business.service.CaSynSignDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Component("caSynSignDataJob")
@Slf4j
public class CaSynSignDataJob {
    @Autowired
    private CaSynSignDataService caSynSignDataService;
    /**
     * CA获取签名值、用户证书接口
     * @return
     */
    public void caSynSignDataJob() {
        log.info("CA获取签名值、用户证书接口定时任务开始执行=================");
        QueryWrapper<CaSynSignData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ywzt", 0);

        List<CaSynSignData> caSynSignDataList = caSynSignDataService.list(queryWrapper);
        if(!CollectionUtils.isEmpty(caSynSignDataList)) {
            log.info("CA获取签名值、用户证书接口定时任务获取到未处理的业务数据条数：{}=================", caSynSignDataList.size());
            String returnStr = "";
            for(CaSynSignData ca : caSynSignDataList) {
                String params = "{\n" +
                        "    \"head\": {\n" +
                        "        \"clientId\": \"{{clientId}}\", \n" +
                        "        \"clientSecret\": \"{{clientSecret}}\"\n" +
                        "    }, \n" +
                        "    \"body\": {\n" +
                        "        \"uniqueId\": \"{{uniqueId}}\", \n" +
                        "        \"isP1\": \"true\"\n" +
                        "    }\n" +
                        "}";
                Integer ywlx = ca.getYwlx();

                if(ywlx == 1) {//病历
                    params = params.replace("{{clientId}}", ca.getClientid());
                    params = params.replace("{{clientSecret}}", ca.getClientsecret());
                    params = params.replace("{{uniqueId}}", ca.getUniqueid());
                    log.info("请求信息：url={}, param={}", ca.getQqdz(), params);
                    returnStr = HttpUtils.doPost(ca.getQqdz(), params);
                    log.info("请求信息：返回值={}", returnStr);
                    if(StringUtils.isNotBlank(returnStr)) {
                        JSONObject jsonObject = new JSONObject(returnStr);
                        //获取成功
                        if(jsonObject.getInt("status") == 0) {
                            JSONObject data = jsonObject.getJSONObject("data");
                            if(!Objects.isNull(data)) {
                                log.info("唯一值{}的签名值：{}", ca.getUniqueid(), data.getStr("p1"));
                                log.info("唯一值{}的证书值：{}", ca.getUniqueid(), data.getStr("cert"));
                                //更新业务状态及业务数据
                                caSynSignDataService.updateCaSynSignDataAndCaSignData(ca, data);
                            }
                        }
                    }
                } else if(ywlx == 2) {//Lis

                }
            }
        }
    }
}